package com.local.runnable.newFixedThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.joda.time.DateTime;
import org.joda.time.Period;

/**
 * newFixedThreadPool
 * 创建固定大小的线程池。
 * 每次提交一个任务就创建一个线程，直到线程达到线程池的最大大小。
 * 线程池的大小一旦达到最大值就会保持不变，如果某个线程因为执行异常而结束，那么线程池会补充一个新线程。
 * @author Administrator
 *
 */
public class NewFixedThread {
	private static int value = 0;
	/**
	 * @param args
	 */
	public static void main(String[] args) {

//		 //创建一个可重用固定线程数的线程池
//        ExecutorService pool = Executors.newFixedThreadPool(5);
//        //创建实现了Runnable接口对象，Thread对象当然也实现了Runnable接口
//        Thread t1 = new MyThread();
//        Thread t2 = new MyThread();
//        Thread t3 = new MyThread();
//        Thread t4 = new MyThread();
//        Thread t5 = new MyThread();
//      //将线程放入池中进行执行
//        pool.execute(t1);
//        pool.execute(t2);
//        pool.execute(t3);
//        pool.execute(t4);
//        pool.execute(t5);
//        //关闭线程池
//       pool.shutdown();
		
		
		 ExecutorService pool = Executors.newFixedThreadPool(3);
		 DateTime dateTime=new DateTime();
		 System.out.println("开始执行:"+dateTime.toString("yyyy-MM-dd HH:mm:ss SSS"));
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程A执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});


			// the method implements Executors: void execute(Runnable command);
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程B执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程C执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
					
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程Z执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程D执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
					
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程E执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程C执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
					
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程K执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程D执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
					
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程T执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程F执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
					
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程G执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程H执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});


			//
			pool.execute(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DateTime dateTime2=new DateTime();
					System.out.println((value++)+"线程I执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
				}
			});
			
			
			pool.shutdown();
			while (true) {  //判断所有线程是否执行完
				if (pool.isTerminated()) {  
					DateTime dateTime2=new DateTime();
					 Period period=new Period(dateTime,dateTime2);
					 Integer hours = period.getHours();
					 Integer minutes = period.getMinutes();
					 Integer seconds = period.getSeconds();
					System.out.println((value)+"所有的任务已执行完毕！花费时间为："+hours+"时"+minutes+"分"+seconds+"秒");
					break;  
				}  
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
			} 
	}

}
